<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/05/0005
 * Time: 11:47
 */

namespace app\sports_admin\service;


use app\exception\PlanException;
use think\Request;
use app\sports_admin\model\Plan as PlanModel;
class Plan
{
    /**
     * 添加教案信息
    */
    public static function planInsert()
    {
        $params = Request::instance()->param();
        $isMotion = self::issetMotion($params['motion']);
        if($isMotion){
            throw new PlanException([
                'msg'=>'教案名称不能重复',
                'errorCode'=>400
            ]);
        }
        $planInfo = self::assemblyInfo($params);
        $planId = PlanModel::create($planInfo);
        if(!$planId){
            throw new PlanException([
                'msg'=>'教案信息添加失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'添加成功']);
    }
    /**
     * 修改教案信息
    */
    public static function planUpdate()
    {
        $params = Request::instance()->param();
        if(trim($params['motion']) != trim($params['primaryMotion'])){
            $isMotion = self::issetMotion($params['motion']);
            if($isMotion){
                throw new PlanException([
                    'msg'=>'教案名称不能重复',
                    'errorCode'=>400
                ]);
            }
        }
        $planInfo = self::assemblyInfo($params);
        //教案id
        $id = $params['id'];
        $planId = (new PlanModel())->save($planInfo,['id'=>$id]);
        if(!$planId){
            throw new PlanException([
                'msg'=>'教案信息修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'修改成功']);
    }
    /**
     * 拼装信息
     * @param array $info 数据表字段信息
    */
    private static function assemblyInfo($info)
    {
        //准备部分总分钟
        $input['one_count_minute'] = $info['one_count_minute'];
        //开始部分总分钟
        $input['two_count_minute'] = $info['two_count_minute'];
        //基本部分总分钟
        $input['three_count_minute'] = $info['three_count_minute'];
        //结束部分总分钟
        $input['end_count_minute'] = $info['end_count_minute'];
        //班级id
        $input['grade_id'] = isset($info['class_id']) ? ','.implode(',',$info['class_id']).',': '';
        //所属运动项目id
      //  $input['project_id'] = $info['project_id'];
        //教案类别id
        $input['plan_type_id'] = $info['plan_type_id'];
        //运动项目名称
        $input['motion'] = trim($info['motion']);
//        //教材分析
//        $input['textbook_analysis'] = htmlspecialchars($info['textbook_analysis']);
        //教学内容
        $input['contents'] = htmlspecialchars($info['contents']);
        //教学难点
        $input['difficulty'] = htmlspecialchars($info['difficulty']);
        //场地器材
        $input['equipment'] = htmlspecialchars($info['equipment']);
        //负荷目标
        $input['load_target'] = htmlspecialchars($info['load_target']);
        //安全措施
        $input['security'] = htmlspecialchars($info['security']);
        //教学目标
        $input['target'] = htmlspecialchars($info['target']);
//        //教学目的
//        $input['objective'] = htmlspecialchars($info['objective']);
//        //教法与学法
//        $input['teaching'] = htmlspecialchars($info['teaching']);
        //教学重点
        $input['teaching_emphasis'] = htmlspecialchars($info['teaching_emphasis']);
        //开始部分
        $input['start_part'] = self::startPart(
            $info['one_title'],$info['one_activity'],$info['one_contents'],$info['one_s_activity'],$info['one_img'],$info['one_video']
        );
        //准备部分
        $input['preparation_part'] = self::preparationPart(
            $info['two_title'],$info['two_activity'],$info['two_contents'],$info['two_s_activity'],$info['two_img'],$info['two_video']
        );
        //基本部分
        $input['basic_part'] = self::basicPart(
            $info['three_activity'],$info['three_contents'],$info['three_s_activity'],$info['three_title'],$info['three_img'],$info['three_video']
        );
        //结束部分
        $input['end_part'] = self::endPart($info['four_activity'],$info['four_contents'],$info['four_s_activity']);
        return $input;
    }
    /**
     * 拼装开始部分字段
     *  @param $oneActivity 教学内容
     * @param $oneContents 教师活动
     * @param $oneSActivity 学生活动
     * @param $oneImg 组织形式图片
     * @param $oneVideo 教学视频地址
    */
    private static function startPart($oneTitle,$oneActivity,$oneContents,$oneSActivity,$oneImg,$oneVideo)
    {
        $startPart = [];
        $images = $oneImg;
        $oneImg = explode(',',$oneImg);
        for($i=0;$i<count($oneActivity);$i++){
            $arr['one_activity'] = htmlspecialchars($oneActivity[$i]);
            $arr['one_contents'] = htmlspecialchars($oneContents[$i]);
            $arr['one_s_activity'] = htmlspecialchars($oneSActivity[$i]);
            $arr['one_title'] = htmlspecialchars($oneTitle[$i]);
            if(isset($oneImg[$i])){
                $arr['one_img'] = $oneImg[$i];
            }else{
                $arr['one_img'] = ',';
                $images = $images.',';
            }
            $arr['one_video'] = $oneVideo[$i] ? $oneVideo[$i] : '';
            $startPart[] = $arr;
        }
        $startPart['one_imgs'] = $images;
        return json_encode($startPart);
//        $startPart = [
//            'one_activity'=>htmlspecialchars($oneActivity),
//            'one_contents'=>htmlspecialchars($oneContents),
//            'one_s_activity'=>htmlspecialchars($oneSActivity),
//            'one_img'=>$oneImg ? $oneImg : '',
//            'one_video'=>$oneVideo ? $oneVideo : ''
//        ];
//        return json_encode($startPart);
    }
    /**
     * 拼装准备部分子段
     * @param $twoActivity 教学内容
     * @param $twoContents 教师活动
     * @param $twoSActivity 学生活动
     * @param $twoImg 组织形式图片
     * @param $twoVideo 教学视频地址
    */
    private static function preparationPart($twoTitle,$twoActivity,$twoContents,$twoSActivity,$twoImg,$twoVideo)
    {

        $preparationPart = [];
        $images = $twoImg;
        $twoImg = explode(',',$twoImg);
        for($i=0;$i<count($twoActivity);$i++){
            $arr['two_activity'] = htmlspecialchars($twoActivity[$i]);
            $arr['two_contents'] = htmlspecialchars($twoContents[$i]);
            $arr['two_s_activity'] = htmlspecialchars($twoSActivity[$i]);
            $arr['two_title'] = htmlspecialchars($twoTitle[$i]);
            if(isset($twoImg[$i])){
                $arr['two_img'] = $twoImg[$i];
            }else{
                $arr['two_img'] = ',';
                $images = $images.',';
            }
            $arr['two_video'] = $twoVideo[$i] ? $twoVideo[$i] : '';
            $preparationPart[] = $arr;
        }
        $preparationPart['two_imgs'] = $images;
        return json_encode($preparationPart);
//        $preparationPart = [
//            'two_activity'=>htmlspecialchars($twoActivity),
//            'two_contents'=>htmlspecialchars($twoContents),
//            'two_s_activity'=>htmlspecialchars($twoSActivity),
//            'two_img'=>$twoImg ? $twoImg : '',
//            'two_video'=>$twoVideo? $twoVideo :""
//        ];
//        return json_encode($preparationPart);
    }
    /**
     * 拼装基本部分字段
     * @param Array $threeActivity 教学内容
     * @param Array $threeContents 教师活动
     * @param Array $threeSActivity 学生活动
     * @param Array $threeTitle 教学名称
     * @param $threeImg 组织形式图片
     * @param Array $threeVideo 教学视频地址
    */
    private static function basicPart($threeActivity,$threeContents,$threeSActivity,$threeTitle,$threeImg,$threeVideo)
    {
        $basicPart = [];
        $images = $threeImg;
        $threeImg = explode(',',$threeImg);
        for($i=0;$i<count($threeActivity);$i++){
            $arr['three_activity'] = htmlspecialchars($threeActivity[$i]);
            $arr['three_contents'] = htmlspecialchars($threeContents[$i]);
            $arr['three_s_activity'] = htmlspecialchars($threeSActivity[$i]);
            $arr['three_title'] = htmlspecialchars($threeTitle[$i]);
            if(isset($threeImg[$i])){
                $arr['three_img'] = $threeImg[$i];
            }else{
                $arr['three_img'] = ',';
                $images = $images.',';
            }
            if(isset($threeVideo[$i])){
                $arr['three_video'] = $threeVideo[$i];
            }else{
                $threeVideo[$i] = '';
            }
            //$arr['three_video'] = $threeVideo[$i] ? $threeVideo[$i] : '';
            $basicPart[] = $arr;
        }
        $basicPart['str_img'] = $images;
        return json_encode($basicPart);
    }
    /**
     * 拼装结束部分字段
     * @param $fourActivity 教学内容
     * @param $fourContents 教师活动
     * @param $fourSActivity 学生活动
    */
    private static function endPart($fourActivity,$fourContents,$fourSActivity)
    {
        $endPart = [
            'four_activity'=>htmlspecialchars($fourActivity),
            'four_contents'=>htmlspecialchars($fourContents),
            'four_s_activity'=>htmlspecialchars($fourSActivity)
        ];
        return json_encode($endPart);
    }
    /*
     * 查询教案名称是否存在
     * @param $motion 教案名称
     */
    private static function issetMotion($motion)
    {
        return PlanModel::where('motion',$motion)->find();
    }
}